import { defineStore } from 'pinia';
import { ref } from 'vue';
import type { Flow, FlowContent, FlowState } from '@/types/flow';
import axios from 'axios';

// 配置后端API的基础URL
const API_BASE_URL = 'http://localhost:8080/api';

export const useFlowStore = defineStore('flow', () => {
  const state = ref<FlowState>({
    flows: [],
    selectedFlow: null,
    flowContent: null,
  });

  const fetchFlows = async () => {
    try {
      console.log('Fetching flows from:', `${API_BASE_URL}/flows`);
      const response = await axios.get(`${API_BASE_URL}/flows`);
      console.log('Response:', response.data);
      state.value.flows = response.data;
    } catch (error) {
      console.error('Error fetching flows:', error);
      throw error;
    }
  };

  const fetchFlowContent = async (flowId: string) => {
    try {
      const response = await axios.get(`${API_BASE_URL}/flows/${flowId}/content`);
      state.value.flowContent = response.data;
    } catch (error) {
      console.error('Error fetching flow content:', error);
      throw error;
    }
  };

  const addFlow = async (flow: Omit<Flow, 'id'>) => {
    try {
      const response = await axios.post(`${API_BASE_URL}/flows`, flow);
      const newFlow = response.data;
      state.value.flows.push(newFlow);
      return newFlow;
    } catch (error) {
      console.error('Error adding flow:', error);
      throw error;
    }
  };

  const deleteFlow = async (flowId: string) => {
    try {
      console.log('Deleting flow:', flowId);
      await axios.delete(`${API_BASE_URL}/flows/${flowId}`);
      console.log('Flow deleted successfully');
      
      // 更新本地状态
      state.value.flows = state.value.flows.filter(flow => flow.id !== flowId);
      if (state.value.selectedFlow?.id === flowId) {
        state.value.selectedFlow = null;
        state.value.flowContent = null;
      }
    } catch (error) {
      console.error('Error deleting flow:', error);
      throw error;
    }
  };

  const selectFlow = (flow: Flow) => {
    console.log('Store: selecting flow:', flow);
    state.value.selectedFlow = flow;
    console.log('Store: fetching flow content for:', flow.id);
    fetchFlowContent(flow.id);
  };

  const updateFlowContent = async (flowId: string, content: string) => {
    try {
      await axios.put(`${API_BASE_URL}/flows/${flowId}/content`, { content });
      // 更新本地状态
      state.value.flowContent = { content };
    } catch (error) {
      console.error('Error updating flow content:', error);
      throw error;
    }
  };

  const smartGenerateFlow = async (prompt: string) => {
    try {
      const response = await axios.post(`${API_BASE_URL}/flows/smart-generate`, { prompt });
      const newFlow = response.data;
      state.value.flows.push(newFlow);
      state.value.selectedFlow = newFlow;
      state.value.flowContent = { content: newFlow.content || '' };
      return newFlow;
    } catch (error) {
      console.error('Error smart generating flow:', error);
      throw error;
    }
  };

  return {
    state,
    fetchFlows,
    fetchFlowContent,
    addFlow,
    deleteFlow,
    selectFlow,
    updateFlowContent,
    smartGenerateFlow, // Add this line
  };
});